package queue;

//消费者生产者
public class Queue {
    private int n;
    boolean flag=false;
    public synchronized int getN() { //线程同步
        if (!flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费:"+n);
        flag=false;//消费完毕,容器中没有数据
        notifyAll();//唤醒所有处于等待的线程, 使他们结束等待
        return n;
    }

    public synchronized void setN(int n) {
        if (flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("生产:"+n);
        this.n = n;
        flag=true;//生产完毕,容器中已经有数据
        notifyAll();
    }
}
